/** @file    CommunicationEvents.cpp
 *  @author  Alessandro Polo
 *  @version $Id: CommunicationEvents.cpp 2808 2010-07-28 20:44:00Z alex $
 *  @brief
 * File containing implementation of classes defined in CommunicationEvents.h
 * The header for this class can be found in CommunicationEvents.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/communication/CommunicationEvents.h>

 #include <core/ObjectAllocator.h>
 #include <core/Variant.h>
 #include <core/List.h>


using namespace std;
namespace wosh {
 namespace communication {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::string getUserName_FromFactContent( const ISerializable* content ) {
	if ( content == NULL ) return "";
	if ( content->isKindOf<Variant>() ) {
		const Variant* val = content->as<Variant>();
		if ( val == NULL ) return "";
		return val->toString();
	 }
	if ( content->isKindOf<List>() ) {
		const List* list = content->as<List>();
		if ( list == NULL ) return "";
		if ( list->size() < 1 ) return "";
		if ( list->itemAt(0) == NULL ) return "";
		const Variant* val = list->itemAt(0)->as<Variant>();
		if ( val == NULL ) return "";
		return val->toString();
	 }
	return "";
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::communication::UserPresence_StateChanged, "wosh::Fact", 1.0, _static_UserPresence_StateChanged )

UserPresence_StateChanged::UserPresence_StateChanged( const std::string& username, User::PRESENCE presence_state, long previous_last_seen )
	: wosh::Fact( _CommunicatorService_EVENT_PresenceChanged ) {
	List* args = List::createFrom<Variant>( username, (int)presence_state, previous_last_seen );
	setData(args);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::string UserPresence_StateChanged::getUserName() const {
	return getUserName_FromFactContent( getData() );
 }

User::PRESENCE UserPresence_StateChanged::getPresenceState() const {
	if ( !hasData() ) return User::PRESENCE_UNKNOWN;
	if ( getData()->isKindOf<List>() ) {
		const List* list = getData()->as<List>();
		if ( list == NULL ) return User::PRESENCE_UNKNOWN;
		if ( list->size() < 2 ) return User::PRESENCE_UNKNOWN;
		if ( list->itemAt(1) == NULL ) return User::PRESENCE_UNKNOWN;
		const Variant* val = list->itemAt(1)->as<Variant>();
		if ( val == NULL ) return User::PRESENCE_UNKNOWN;
		if ( !val->isNumeric() ) return User::PRESENCE_UNKNOWN;
		return (User::PRESENCE)val->toInteger((int)User::PRESENCE_UNKNOWN);
	 }
	return User::PRESENCE_UNKNOWN;
 }

long UserPresence_StateChanged::getPreviousLastSeen() const {
	if ( !hasData() ) return 0;
	if ( getData()->isKindOf<List>() ) {
		const List* list = getData()->as<List>();
		if ( list == NULL ) return 0;
		if ( list->size() < 3 ) return 0;
		if ( list->itemAt(2) == NULL ) return 0;
		const Variant* val = list->itemAt(2)->as<Variant>();
		if ( val == NULL ) return 0;
		if ( !val->isNumeric() ) return 0;
		return val->toInteger(0);
	 }
	return 0;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::communication::UserInteraction_Event, "wosh::Fact", 1.0, _static_UserInteraction_Event )

UserInteraction_Event::UserInteraction_Event( const std::string& username )
	: wosh::Fact( _CommunicatorService_EVENT_UserInteraction ) {
	setData( new Variant(username) );
 }

std::string UserInteraction_Event::getUserName() const {
	return getUserName_FromFactContent( getData() );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::communication::UserLogging_Event, "wosh::Fact", 1.0, _static_UserLogging_Event )

UserLogging_Event::UserLogging_Event( const std::string& username )
	: wosh::Fact( _CommunicatorService_EVENT_UserLogging ) {
	setData( new Variant(username) );
 }

std::string UserLogging_Event::getUserName() const {
	return getUserName_FromFactContent( getData() );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace communication
}; // namespace wosh
